<route lang="jsonc" type="page">
{
    "layout": "default",
    "style": {
        "navigationBarTitleText": "库存信息",
        // "navigationStyle": "custom"
    }
}
</route>

<script setup lang="ts">
import dayjs from 'dayjs'
import { ref } from 'vue'
import { useMessage, useToast } from 'wot-design-uni'

import { ResInventoryApi } from '@/api/wms/MaterialsManagement/resinventory'
import { ResMaterialApi } from '@/api/wms/MaterialsManagement/resmaterial'
import { RackLocationApi } from '@/api/wms/WarehouseManagement/RackLocation/index'
import { WarehouseRackApi } from '@/api/wms/WarehouseManagement/WarehouseRack/index'
import Pagination from '@/components/pagination/index.vue'
import { getIntDictOptions } from '@/utils/dict'

const message = useMessage()
const toast = useToast()

// 确保列配置的 prop 唯一性

// 响应式数据
const materialList = ref<any[]>([]) // 物资列表
const warehouseList = ref<any[]>([]) // 仓库列表
const warehouseRackList = ref<any[]>([]) // 货架列表
const rackLocationList = ref<any[]>([]) // 库位列表
const list = ref<any[]>([]) // 列表的数据
const queryParams = ref<any>({
  pageNo: 1,
  pageSize: 10,
  areaCode: undefined,
//   type: undefined,
})
const total = ref(0)
const loading = ref(false)

// const userList = ref<any[]>([])
const searchType = ref<any>('仓库')
const value = ref<any>('')
const menu = ref<any[]>([

  {
    content: '仓库',
    prop: 'areaCode',
  },
  {
    content: '物资',
    prop: 'materialId',
  },
])
function changeSearchType({ item }: { item: any }) {
  searchType.value = item?.content
}
function search() {
  // 找到选中的搜索字段
  const selectedMenu = menu.value.find(item => item.content === searchType.value)
  if (selectedMenu) {
    // 清空之前的搜索条件
    queryParams.value.userName = undefined
    queryParams.value.purchasingManager = undefined
    // 根据选中的搜索类型设置搜索值
    queryParams.value[selectedMenu.prop] = value.value
    // 重新获取数据
    fetchList()
  }
}

function getDict(type: any, value: any) {
  const dicts = getIntDictOptions(type)
  const dict = dicts.find(item => item.value === value)
  return dict ? dict.label : '待审批'
}
// 切换分页
function changePage(newPage: any) {
  queryParams.value.pageNo = newPage
  fetchList()
}

// 获取库存列表
async function fetchList() {
  loading.value = true
  try {
    const response = await ResInventoryApi.getResInventoryPage(queryParams.value)
    console.log('获取库存列表', response)

    list.value = (response?.data as any)?.list as any || []
    console.log('库存列表数据', list.value)

    total.value = (response?.data as any)?.total as any || 0
  }
  catch (error) {
    console.error(error)
  }
  finally {
    loading.value = false
  }
}

/** 点击一整行跳转至详情页面 */
function handleClick({ rowIndex }: any) {
  const id = list.value[rowIndex]?.id
  if (id) {
    uni.navigateTo({ url: `/pages/inventory/inventoryInfo/detail?id=${id}` })
  }
}

async function handleSearch(warehouseId: any) {
  console.log('warehouseId', warehouseId)

  queryParams.value.areaCode = warehouseId.value
  fetchList()
}
onLoad(async () => {
  const [warehouseRes, rackRes, locationRes, materialRes] = await Promise.all([
    WarehouseRackApi.getWarehouseList(),
    RackLocationApi.getWarehouseRackList(),
    RackLocationApi.getRackLocationList(),
    ResMaterialApi.getResMaterialList(),

  ])

  materialList.value = materialRes.data as any[]
  warehouseList.value = warehouseRes.data as any[]
  warehouseRackList.value = rackRes.data as any[]
  rackLocationList.value = locationRes.data as any[]
  fetchList()
})
</script>

<template>
  <view>
    <!-- <wd-search v-model="value" hide-cancel placeholder-left placeholder="请输入关键字" @search="search">
      <template #prefix>
        <wd-popover mode="menu" :content="menu" @menuclick="changeSearchType">
          <view class="search-type">
            <text>{{ searchType }}</text>
            <wd-icon custom-class="icon-arrow" name="fill-arrow-down" />
          </view>
        </wd-popover>
      </template>
    </wd-search> -->
    <view class="mx-2 my-2" style="display:flex; gap:8px; align-items:center;">
      <view>
        <wd-icon name="search" size="22px" />
      </view>
      <view class="flex-1">
        <wd-select-picker
          v-model="queryParams.areaCode" label="请输入仓库搜索"
          label-width="150px" prop="formData.areaCode" :columns="warehouseList" placeholder="请选择目标仓库" value-key="id" label-key="wareName" :clearable="true"
          type="radio" filterable :show-confirm="false" @change="handleSearch"
        />
      </view>
    </view>

    <wd-table :data="list" :height="600" :border="false" @row-click="handleClick">
      <!-- 表格列定义 -->
      <wd-table-col
        prop="materialId" label="物资"
        width="160" align="center"
      >
        <template #value="{ row }">
          {{ materialList.find(item => item.id === row.materialId)?.materialName || '-' }}
        </template>
      </wd-table-col>
      <wd-table-col
        prop="areaCode" label="所在仓库"
        width="120" align="center"
      >
        <template #value="{ row }">
          {{ warehouseList.find(item => item.id === row.areaCode)?.wareName || '-' }}
        </template>
      </wd-table-col>
      <wd-table-col
        prop="shelfCode" label="所在货架"
        width="100" align="center"
      >
        <template #value="{ row }">
          {{ warehouseRackList.find(item => item.id === row.shelfCode)?.rackName || '-' }}
        </template>
      </wd-table-col>
      <wd-table-col
        prop="inboundTime" label="入库时间"
        width="120" align="center"
      >
        <template #value="{ row }">
          {{ row.inboundTime ? dayjs(row.inboundTime).format('YYYY年MM月DD日 HH:mm:ss') : '-' }}
        </template>
      </wd-table-col>
    </wd-table>

    <!-- 分页组件 -->
    <Pagination
      :total="total" :page-no="queryParams.pageNo" :page-size="queryParams.pageSize"
      @update:page-no="changePage"
    />
  </view>
</template>

<style lang="scss" scoped>
//
</style>

<style scoped lang="scss">
.container {
  padding: 20px 10px;
}

.search-type {
  position: relative;
  height: 30px;
  line-height: 30px;
  padding: 0 8px 0 16px;
}

.search-type::after {
  position: absolute;
  content: '';
  width: 1px;
  right: 0;
  top: 5px;
  bottom: 5px;
  background: rgba(0, 0, 0, 0.25);
}

.search-type {
  :deep(.icon-arrow) {
    display: inline-block;
    font-size: 20px;
    vertical-align: middle;
  }
}
</style>
